package com.junjie.controller;

import com.alibaba.fastjson.JSONObject;
import com.junjie.entity.Apartment;
import com.junjie.services.ApartmentService;
import com.junjie.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/apartment")
@ResponseBody
public class ApartmentController {

    @Autowired
    private ApartmentService apartmentService;

    //万能查询公寓的信息
    @RequestMapping(value = "/selectApartment", method = RequestMethod.POST)
    public CommonResult selectApartment(@RequestBody JSONObject json) {
        Apartment apartment = new Apartment();
        apartment.setId(json.getInteger("id"));
        apartment.setAptId(json.getInteger("apt_id"));
        apartment.setAptName(json.getString("apt_name"));
        apartment.setAptLog(json.getString("apt_log"));
        apartment.setAId(json.getInteger("a_id"));
        return new CommonResult(200, "查询成功！", apartmentService.selectApartment(apartment));
    }

    //查询一条公寓信息
    @RequestMapping(value = "/getOneApartment", method = RequestMethod.POST)
    public CommonResult getOneApartment(@RequestBody JSONObject json) {
        Apartment apartment = new Apartment();
        apartment.setId(json.getInteger("id"));
        apartment.setAptName(json.getString("apt_name"));
        apartment.setAptLog(json.getString("apt_log"));
        apartment.setAId(json.getInteger("a_id"));
        return new CommonResult(200, "查询成功！", apartmentService.getOneApartment(apartment));
    }

    //增加一条公寓信息
    @RequestMapping(value = "/addApartment", method = RequestMethod.POST)
    public CommonResult addApartment(@RequestBody JSONObject json) {
        Apartment apartment = new Apartment();
        apartment.setAId(json.getInteger("apt_id"));
        apartment.setAptName(json.getString("apt_name"));
        apartment.setAptLog(json.getString("apt_log"));
        apartment.setAId(json.getInteger("a_id"));
        return apartmentService.addApartment(apartment);
    }

    //修改该一条公寓信息
    @RequestMapping(value = "/updateApartment", method = RequestMethod.POST)
    public CommonResult updateApartment(@RequestBody JSONObject json) {
        Apartment apartment = new Apartment();
        apartment.setId(json.getInteger("id"));
        apartment.setAId(json.getInteger("apt_id"));
        apartment.setAptName(json.getString("apt_name"));
        apartment.setAptLog(json.getString("apt_log"));
        apartment.setAId(json.getInteger("a_id"));
        return apartmentService.updateApartment(apartment);
    }

    //删除一条公寓的信息
    @RequestMapping(value = "/updateApartment/{id}", method = RequestMethod.POST)
    public CommonResult deleteApartment(@PathVariable("id") Integer id) {
        return apartmentService.deleteApartment(id);
    }
}
